package com.yunji.aggregate.service;

import com.yunji.aggregate.cache.AggregateCache;
import com.yunji.aggregate.mapper.IStockAggregateMapper;
import com.yunji.aggregate.model.dto.base.BelongStockParam;
import com.yunji.aggregate.model.dto.stock.*;
import com.yunji.aggregate.model.vo.base.BelongStockStatisticsVO;
import com.yunji.aggregate.model.vo.stock.*;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.business.StorageStatus;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class StockAggregateService {

    @Resource
    private IStockAggregateMapper stockAggregateMapper;


    public List<BelongStockStatisticsVO> belongStockList(BelongStockParam param) {
        return stockAggregateMapper.belongStockList(param);
    }

    public List<StockStatisticsVO> searchStockList(StockParam param) {
        param.setParam(param.getBelongIds());

        List<StockStatisticsVO> list = stockAggregateMapper.searchStockList(param);
        if (BeanUtils.isNull(list)) {
            return new ArrayList<>();
        }
        for (StockStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
        }
        return list;
    }

    public List<AllocateTicketStatisticsVO> searchAllocateList(AllocateTicketParam param) {
        Assert.isTrue(BeanUtils.isNotNull(SystemContext.getBelongId()), "无权访问");
        param.setDraftBelongId(SystemContext.getBelongId());
        param.setParam(param.getBelongIds());

        List<AllocateTicketStatisticsVO> list = stockAggregateMapper.searchAllocateList(param);
        if (BeanUtils.isNull(list)) {
            return new ArrayList<>();
        }
        for (AllocateTicketStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
            vo.setIntoBelongName(AggregateCache.getBelongName(vo.getIntoBelongId()));
            vo.setOutBelongName(AggregateCache.getBelongName(vo.getOutBelongId()));
        }
        return list;
    }

    public List<DiscardTicketStatisticsVO> searchDiscardList(DiscardTicketParam param) {
        Assert.isTrue(BeanUtils.isNotNull(SystemContext.getBelongId()), "无权访问");
        param.setDraftBelongId(SystemContext.getBelongId());
        param.setParam(param.getBelongIds());
        List<DiscardTicketStatisticsVO> list = stockAggregateMapper.searchDiscardList(param);
        if (BeanUtils.isNull(list))
            return new ArrayList<>();
        for (DiscardTicketStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
            vo.setOutBelongName(AggregateCache.getBelongName(vo.getOutBelongId()));
        }
        return list;
    }

    public List<RepairTicketStatisticsVO> searchRepairList(RepairTicketParam param) {
        Assert.isTrue(BeanUtils.isNotNull(SystemContext.getBelongId()), "无权访问");
        param.setDraftBelongId(SystemContext.getBelongId());
        param.setParam(param.getBelongIds());

        List<RepairTicketStatisticsVO> list = stockAggregateMapper.searchRepairList(param);
        if (BeanUtils.isNull(list))
            return new ArrayList<>();
        for (RepairTicketStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
            vo.setOutBelongName(AggregateCache.getBelongName(vo.getOutBelongId()));
            vo.setIntoBelongName(AggregateCache.getBelongName(vo.getIntoBelongId()));
        }
        return list;
    }

    public List<StockTakeStatisticsVO> searchStockTakeList(StockTakeParam param) {
        Assert.isTrue(BeanUtils.isNotNull(SystemContext.getBelongId()), "无权访问");
        param.setDraftBelongId(SystemContext.getBelongId());
        param.setParam(param.getBelongIds());
        List<StockTakeStatisticsVO> list = stockAggregateMapper.searchStockTakeList(param);
        if (BeanUtils.isNull(list))
            return new ArrayList<>();
        for (StockTakeStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
        }
        return list;
    }

    public List<StorageTicketStatisticsVO> searchStorageTicketList(StorageTicketParam param) {
        param.setUseStatus(LogicCode.NO);
        param.setParam(param.getBelongIds());
        List<StorageTicketStatisticsVO> list = stockAggregateMapper.searchStorageTicketList(param);
        if (BeanUtils.isNull(list)) {
            return new ArrayList();
        }
        for (StorageTicketStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
        }
        return list;
    }

    public List<StorageRecordsStatisticsVO> searchRecordsList(StorageRecordsParam param) {
        Integer[] storageStatus = new Integer[]{
                StorageStatus.ALREADY_STORAGE.getCode(),
                StorageStatus.PART_STORAGE.getCode()
        };
        param.setStorageStatus(storageStatus);
        param.setParam(param.getBelongIds());
        List<StorageRecordsStatisticsVO> list = stockAggregateMapper.searchRecordsList(param);
        if (BeanUtils.isNull(list)) {
            return new ArrayList<>();
        }
        for (StorageRecordsStatisticsVO vo : list) {
            vo.setBelongName(AggregateCache.getBelongName(vo.getBelongId()));
        }
        return list;
    }

    /*--------------------------------------------------------模糊查询-------------------------------------------------------*/
    public List<String> searchProductName(StockParam param) {
        param.setSearchField("pro.product_name");
        LoginInfo user = SystemContext.getUser();
        String userType = user.getUserType();
        Long belongId = user.getBelongId();
        if (Objects.equals(UserType.AGENT.getCode(), userType)) {
            param.setParentId(belongId);
        }
        if (Objects.equals(UserType.SITE.getCode(), userType)) {
            param.setBelongId(belongId);
        }
        List<String> list = stockAggregateMapper.searchStockTableField(param);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }

    public List<String> searchProductName(StorageRecordsParam param) {
        LoginInfo user = SystemContext.getUser();
        String userType = user.getUserType();
        Long belongId = user.getBelongId();
        if (Objects.equals(UserType.AGENT.getCode(), userType)) {
            param.setParentId(belongId);
        }
        if (Objects.equals(UserType.SITE.getCode(), userType)) {
            param.setBelongId(belongId);
        }
        param.setSearchField("pro.product_name");
        List<String> list = stockAggregateMapper.searchStorageRecordsTableField(param);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }

}
